;
(function() {
	var skill = {
		_getEvaluation: function(evaluation_id) {
			var evaluations = this.evaluations || (this.evaluations = []);
			if (evaluations.indexOf(evaluation_id) === -1) {
				throwCusError("评价不属于指定的对象");
			}
			var evaluation = EvaluationCon.get(evaluation_id);
			if (!evaluation) {
				throwCusError("找不到指定评价");
			}
			return evaluation;
		},
		//获取详细评价列表
		getEvaluations: function(num, page) {
			var evaluations = this.evaluations || (this.evaluations = []);
			evaluations = evaluations.map(function(evaluation_id, index) {
				var evaluation = EvaluationCon.get(evaluation_id);
				return evaluation.getDetail();
			});
			return evaluations;
		},
		//带着页面信息的评价信息
		getEvaluations_PageInfo: function(num, page) {
			var result = this.getEvaluations();
			return {
				total_num: result.length,
				total_page: Math.round(result.length / num) || 1, //至少一页
				evaluations: data_cut.page(result, num, page),
				page: page || 0,
				num: num || 0
			}
		},
		// getOneEvaluationDetail:function (eval_id) {
		// 	evaluation = EvaluationCon.get(eval_id);
		// 	if (!evaluation) {
		// 		throwCusError("找不到指定评价");
		// 	}
		// 	return evaluation.getDetail();
		// },
		//判断是否已经评价过这个商品
		hasEvaluation: function(goods_id) {
			//如果全部通过!=判断，说明没有评价过这个商品
			return !this.getEvaluations().every(function(evaluation) {
				return evaluation.goods_id != goods_id;
			});
		},
		//判断能否评价这个商品
		canEvaluation: function(goods_id) {
			//如果全部通过!=判断，说明没有买过
			return !this.getAchs().every(function(ach) {
				return ach.info.goods_info.every(function(goods_info) {
					return goods_info.goods._id != goods_id
				})
			});
		},
		toEvaluation: function(goods_id) {
			if (!GoodsCon.get(goods_id)) {
				throwCusError("找不到指定商品");
			}
			if (this.canEvaluation(goods_id)) {
				// if (this.hasEvaluation(goods_id)) {
				// 	throwCusError("已经评价过此商品，不能多次评价");
				// }
			} else {
				throwCusError("未购买过此商品，无评价权限");
			}
			return true;
		},
		//添加评价
		addEvaluation: function(goods_id, star_num, content, isAny /*是否匿名*/ ) {
			this.toEvaluation(goods_id);
			var comment = new CommentCon({
				type: "user",
				commenter_id: this._id,
				content: content,
				isAny: isAny
			});
			var evaluation = new EvaluationCon({
				goods_id: goods_id,
				star_num: star_num,
				comment_id: comment._id
			});
			return evaluation._id;
		},
		//添加评价，非安全
		_addEvaluation: function(evaluation_id) {
			var evaluations = this.evaluations || (this.evaluations = []);
			evaluations.push(evaluation_id);
			this.save();
		},
		updateEvaltion: function(evaluation_id, evaluation_info) {
			// var evaluation = this._getEvaluation(evaluation_id);
			throwCusError("评价不可修改")
		},
		removeEvaluation: function(evaluation_id) {
			var evaluation = this._getEvaluation(evaluation_id);
			evaluation.destroy();
			this.evaluations = $Array.remove(this.evaluations, evaluation_id);
			this.save();
		},
	};
	module.exports = skill;
	var db = require("../db/index");
	var UserCon = require("./_con");
	var UserCartGoodsCon = require("../user.cart_goods/_con");
	var UserAddressCon = require("../user_address/_con");
	var BusCon = require("../bus/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var GoodsCon = require("../goods/_con");
	var AchCon = require("../ach/_con");
	var PayAchCon = require("../pay_ach/_con");
	var CommentCon = require("../comment/_con");
	var EvaluationCon = require("../eval/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var $Array = require("../_include/$.Array");
	var $Object = require("../_include/$.Object");
	var md5 = require("../_include/md5");
	var helper = require("../_include/helper");
}());